//
//  CTCallViewController.m
//  CT
//
//  Created by liufanwei on 15/4/13.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTCallViewController.h"
#import "CTUserDefaultHelper.h"
#import "CTNetHelper.h"
#import "BDKNotifyHUD.h"
#import "CTDataManager.h"

@interface CTCallViewController ()
{
    NSTimer * _Timer;
    UIImageView * _BgImage;
}
@property (weak, nonatomic) IBOutlet UILabel *Comefrom;

@end

@implementation CTCallViewController
@synthesize ItemData;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * caller = [[CTUserDefaultHelper ShareInstance] Account];
    NSString * callees = ItemData.PhoneNumber;
    if (!callees) {
        [self dismissViewControllerAnimated:YES completion:^{}];
        return;
    }
    _BgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
    _BgImage.frame = self.view.frame;
    [self.view insertSubview:_BgImage atIndex:0];
    BOOL b = [[CTUserDefaultHelper ShareInstance] ShowNumber];
    NSString * isShowNumber = [NSString stringWithFormat:@"%d",b];
    [[CTNetHelper ShareInstance] makeCall:@{@"caller":caller, @"callees":callees, @"isShowNumber":isShowNumber} WithCallBack:^(id data) {
        NSString * msg = [data objectForKey:@"msg"];
        NSInteger code = [[data objectForKey:@"code"] integerValue];
        if(code == 0){
            NSString * province = [data objectForKey:@"province"];
            NSString * cityname = [data objectForKey:@"cityname"];
            NSString * isp      = [data objectForKey:@"isp"];
            NSString * str = [NSString stringWithFormat:@"通话提交成功\n请等待回拨\n\n%@ %@\n%@\n%@",province,cityname,isp,ItemData.PhoneNumber];
            _Comefrom.text = str;
            
            ItemData.Date = [NSDate date];
            ItemData.Addr = [NSString stringWithFormat:@"%@-%@-%@",isp, province,cityname];
            [[[CTDataManager shareInstance] HistoryManager] addItem:ItemData];
            [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
        }else{
            [BDKNotifyHUD notifyHUDWithText:msg];
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }
    }];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [_Timer invalidate];
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)timerFired:(NSTimer *)Timer{
    [Timer invalidate];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
